﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace bezeq_online_work.view
{
    public partial class workform : Form
    {
        CONTROL.BusinessLogic controler;
        DateTime currentShowen;

        public workform(CONTROL.BusinessLogic d)
        {
            InitializeComponent();
            controler = d;
            try
            {
                List<string>[] shifts=controler.retriveWorkArregnmanetThis();
                populategridFrames(shifts);
               
                DateTime dd = DateTime.Today;
                int offset = dd.DayOfWeek - DayOfWeek.Sunday;
                DateTime lastsunday = dd.AddDays(-offset);
                currentShowen = lastsunday;
                string week = lastsunday.ToShortDateString();
                workweek.Text = week + " - " + lastsunday.AddDays(6).ToShortDateString(); 

            }
            catch (Exception ex)
            {
                // throw error message
                MessageBox.Show("an error has occurred. contact your administrator for further assistance " + ex.Message);
            }
          
            
        }

        public void populategridFrames(List<string>[] shifts)
        {
            int pivot1 = 0;
            int pivot2 = 0;
            for (int i = 0; i < 14; i++)
            {
                pivot1 = 0 ;
                pivot2 = 0 ;
                foreach (string name in shifts[i])
                {
                  
                    if ((i % 2) == 0)
                    {
                        pivot1++;
                        if (mishmarot1.RowCount < pivot1)
                        { this.mishmarot1.Rows.Add(); }
                        this.mishmarot1.Rows[pivot1-1].Cells[(i/2)%7].Value = name;
                    
                    }
                    else
                    {
                        pivot2++;
                        if (mishmarot2.RowCount < pivot2)
                        { this.mishmarot2.Rows.Add(); }
                        this.mishmarot2.Rows[pivot2 - 1].Cells[(i / 2) % 7].Value = name;
                    }
                }
            }
        
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            view.mainmenu_ t = new mainmenu_( controler );
            t.Show();
            this.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            mishmarot1.Rows.Clear();
            mishmarot1.Refresh();
            mishmarot2.Rows.Clear();
            mishmarot2.Refresh();
            currentShowen=currentShowen.AddDays(7);
            DateTime dd = currentShowen;
            string week = dd.ToShortDateString();
            workweek.Text = week + " - " + dd.AddDays(6).ToShortDateString(); 
             try
            {
                List<string>[] shifts = controler.retriveWorkArregnmanet(currentShowen.ToString("yyyy-MM-ddTHH:mm:ss.fff"));
                populategridFrames(shifts);

            }
             catch (Exception ex)
             {
                 // throw error message
                 MessageBox.Show("an error has occurred. contact your administrator for further assistance " + ex.Message);
             }
          
        }

        private void button3_Click(object sender, EventArgs e)
        {

            mishmarot1.Rows.Clear();
            mishmarot1.Refresh();
            mishmarot2.Rows.Clear();
            mishmarot2.Refresh();
            currentShowen = currentShowen.AddDays(-7);
            DateTime dd = currentShowen;
            string week = dd.ToShortDateString();
            workweek.Text = week + " - " + dd.AddDays(6).ToShortDateString();
            try
            {
                List<string>[] shifts = controler.retriveWorkArregnmanet(currentShowen.ToString("yyyy-MM-ddTHH:mm:ss.fff"));
                populategridFrames(shifts);

            }
            catch (Exception ex)
            {
                // throw error message
                MessageBox.Show("an error has occurred. contact your administrator for further assistance " + ex.Message);
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            MessageBox.Show("לא מומש עדיין");
        }
    }
}
